import mx.controls.Alert;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;

import org.fluidindia.financial.ria.event.LoginEvent;
import org.fluidindia.ria.library.event.ViewChangeEvent;
import org.fluidindia.security.vo.LoginVO;


	private function isFormValid():Boolean
	{
		var isValid:Boolean = true;
		if (txtUsername.text.length == 0 || txtPassword.text.length == 0)
		{
			lblErrorMessages.text = "Username and Password are mandatory";
			isValid = false;
		}
		
		return isValid; 
	}


	private function login() : void
	{
		if (isFormValid())
		{
			var event : LoginEvent = new LoginEvent ( LoginEvent.LOGIN_EVENT, handleLoginResult, handleLoginFault );
			event.username = txtUsername.text;
			event.password = txtPassword.text;
			dispatchEvent( event );
			
			dispatchEvent(new ViewChangeEvent("2.1"));
			doCleanup();
		}
	}

	private function handleLoginResult(event:ResultEvent) : void
	{
		var loginResponse:LoginVO = LoginVO (event.result);
		dispatchEvent(new ViewChangeEvent("2.1"));
	}


	private function handleLoginFault(event:FaultEvent):void
	{
		Alert.show( event.fault.faultString, 'Failure' );
	}

	private function doCleanup():void
	{
		txtUsername.text = "";
		txtPassword.text = "";
		lblErrorMessages.text = "";
	}



